package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.ProductDao;
import com.itheima.domain.store.Product;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.ProductService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProductServiceImpl implements ProductService {

    @Override
    public List<Product> findAll() {
        SqlSession sqlSession = null;
        List<Product> list= null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            ProductDao mapper = MapperFactory.getMapper(sqlSession, ProductDao.class);
            list = mapper.findAll();

        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public Product findById(String id) {
        SqlSession sqlSession = null;
        Product product = null;

        //try {
            sqlSession =  MapperFactory.getSqlSession();
            ProductDao mapper = MapperFactory.getMapper(sqlSession, ProductDao.class);
            product = mapper.findById(id);


//        } catch (Exception e) {
//            System.out.println("回滚了");
//            TransactionUtil.rollback(sqlSession);
//        } finally {
//            TransactionUtil.close(sqlSession);
//        }
        return product;
    }

    @Override
    public void addProduct(Product product) {
        SqlSession sqlSession = null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            ProductDao mapper = MapperFactory.getMapper(sqlSession, ProductDao.class);
            mapper.save(product);


        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void deleteProduct(String id) {
        SqlSession sqlSession = null;

        //try {
        sqlSession =  MapperFactory.getSqlSession();
        ProductDao mapper = MapperFactory.getMapper(sqlSession, ProductDao.class);
        if(id.contains(",")){
            String[] split = id.split(",");
            for (String s : split) {
                mapper.delete(id);
            }
        }else{
            mapper.delete(id);
        }
//        } catch (Exception e) {
//            System.out.println("回滚了1");
//            TransactionUtil.rollback(sqlSession);
//        } finally {
//            TransactionUtil.close(sqlSession);
//        }

    }

    @Override
    public void updateProduct(Product product) {

        SqlSession sqlSession = null;

        try {
            sqlSession =  MapperFactory.getSqlSession();
            ProductDao mapper = MapperFactory.getMapper(sqlSession, ProductDao.class);
            mapper.update(product);

        } catch (Exception e) {
            System.out.println("回滚了");
            TransactionUtil.rollback(sqlSession);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }


    @Override
    public PageInfo findByPage(int currentPage, int pageSize) {

//        开启分页

        SqlSession sqlSession = null;
        PageInfo pageInfo = null;


        //try {
            sqlSession =  MapperFactory.getSqlSession();
            ProductDao mapper = MapperFactory.getMapper(sqlSession, ProductDao.class);

            //PageHelper.startPage(1,5);
            PageHelper.startPage(currentPage,pageSize);
            List<Product> all = mapper.findAll();
            System.out.println(all);
//            使用分页插件进行分页处理
            pageInfo = new PageInfo(all);

//        } catch (Exception e) {
//            System.out.println("回滚了");
//            TransactionUtil.rollback(sqlSession);
//        } finally {
//            TransactionUtil.close(sqlSession);
//        }

        return pageInfo;
    }
}
